class PostsController < ApplicationController
    before_action :find_post , except:[:index,:new,:create]

	def index
		@posts = Post.all.by_last_updated_at
	end

	def show

	end

	def new
		@post = Post.new
	end

	def create
		@post = Post.create(post_params)
		if @post.save
			redirect_to posts_path
		else
			render :new
		end
	end

	def edit
		
	end

	def update
		if @post.update(post_params)
			redirect_to post_path(@post),notice: "修改成功"
		else
			render :edit
		end
		
	end

	def destroy
        @post.destroy
        redirect_to posts_path, alert: "已刪除"
	end

	

	private

    def find_post
		@post = Post.find(params[:id])
	end

	def post_params
		params.require(:post).permit(:title, :content)
	end
end
